package com.spider.spiderflowable.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spider.spiderflowable.business.entity.dto.UserDTO;
import com.spider.spiderflowable.business.entity.dto.UserPasswordDTO;
import com.spider.spiderflowable.business.entity.model.User;
import com.spider.spiderflowable.business.entity.param.UserQueryParam;
import com.spider.spiderflowable.business.service.UserService;
import com.spider.spiderflowable.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiong.bo
 * @version 1.0
 * @date 2020/11/18 17:08
 */
@Tag(name = "用户模块")
@RequestMapping("/system/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Operation(summary = "用户列表")
    @GetMapping
    public Result<Page<User>> selectPage(UserQueryParam userQueryParam) {
        return Result.of(userService.selectPage(userQueryParam));
    }

    @Operation(summary = "用户详情")
    @GetMapping("/{id}")
    public Result<UserDTO> findById(@PathVariable Long id) {
        return Result.of(userService.findUserDtoById(id));
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/current")
    public Result<User> findCurrentUSer() {
        return Result.of(userService.getUserContent());
    }

    @Operation(summary = "新增用户")
    @PostMapping
    public Result<User> save(@RequestBody UserDTO user) {
        return Result.of(userService.saveUser(user));
    }

    @Operation(summary = "修改用户")
    @PutMapping
    public Result<User> update(@RequestBody UserDTO user) {
        return Result.of(userService.updateUser(user));
    }

    @Operation(summary = "启用/禁用用户")
    @PostMapping("/enable/{id}/{status}")
    public Result<User> enableUser(@PathVariable Long id, @PathVariable Integer status) {
        return Result.of(userService.enableUser(id, status));
    }

    @Operation(summary = "用户修改密码")
    @PostMapping("updatePassword")
    public Result<User> updatePassword(@RequestBody UserPasswordDTO userPasswordDto) {
        return Result.of(userService.updatePassword(userPasswordDto));
    }

    @Operation(summary = "管理员给用户重置密码")
    @PostMapping("resetAccountPassword")
    public Result<User> resetAccountPassword(@RequestBody UserPasswordDTO userPasswordDto) {
        return Result.of(userService.resetAccountPassword(userPasswordDto));
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/delete")
    public Result<Boolean> delete(@RequestParam("ids") String ids) {
        return Result.of(userService.deleteUser(ids));
    }


}
